import GraphBase from "./graphBase";
export default class RectNodeGraph extends GraphBase {
  position: {
    x: number;
    y: number;
  } = { x: 0, y: 0 };
  leftTop: {
    x: number;
    y: number;
  } = { x: 0, y: 0 };
  width: number = 50;
  height: number = 50;
  rotation: number = 0;
  background: string = "rgb(64,158,255)";
  isFill = true;
  type = "rect";
  private originRect = { x: 0, y: 0, width: 0, height: 0 };
  constructor() {
    super();
  }
  draw() {
    if (this.ctx) {
      this.ctx.fillStyle = this.background;
      if (this.isFill) {
        this.ctx.fillRect(
          this.leftTop.x,
          this.leftTop.y,
          this.width,
          this.height
        );
      } else {
        this.ctx.strokeRect(
          this.leftTop.x,
          this.leftTop.y,
          this.width,
          this.height
        );
      }
    }
  }
  contain(x: number, y: number): boolean {
    if (this.isFill) {
      if (
        x >= this.leftTop.x &&
        x <= this.leftTop.x + this.width &&
        y >= this.leftTop.y &&
        y <= this.leftTop.y + this.height
      ) {
        return true;
      }
    } else {
      if (
        ((x === this.leftTop.x || x === this.leftTop.x + this.width) &&
          y >= this.leftTop.y &&
          y <= this.leftTop.y + this.height) ||
        ((y === this.leftTop.y || y === this.leftTop.y + this.height) &&
          x >= this.leftTop.x &&
          x <= this.leftTop.x + this.width)
      ) {
        return true;
      }
    }
    return false;
  }
  setOriginRect(x: number, y: number, width: number, height: number) {
    this.originRect.x = x;
    this.originRect.y = y;
    this.originRect.width = width;
    this.originRect.height = height;
  }
  getOriginRect() {
    return {
      x: this.originRect.x,
      y: this.originRect.y,
      width: this.originRect.width,
      height: this.originRect.height,
    };
  }
  setWidth(width: number) {
    this.width = width;
  }
  setHeight(height: number) {
    this.height = height;
  }
  setPosition(x: number, y: number) {
    this.position.x = x;
    this.position.y = y;
    this.leftTop.x = x - this.width / 2;
    this.leftTop.y = y - this.height / 2;
  }
  setLeftTop(x: number, y: number) {
    this.leftTop.x = x;
    this.leftTop.y = y;
    this.position.x = x + this.width / 2;
    this.position.y = y + this.height / 2;
  }
  setRotation(rotation: number) {
    this.rotation = rotation;
  }
  setBackgroundColor(color: string) {
    this.background = color;
  }
  getRect(): {
    x: number;
    y: number;
    width: number;
    height: number;
  } {
    return {
      x: this.leftTop.x,
      y: this.leftTop.y,
      width: this.width,
      height: this.height,
    };
  }
  translate(x: number, y: number) {
    const originRect = this.getOriginRect();
    this.setLeftTop(originRect.x + x, originRect.y + y);
  }
  offset(x: number, y: number) {
    this.setLeftTop(this.leftTop.x + x, this.leftTop.y + y);
  }
  scale(width: number, height: number) {
    this.setWidth(this.width * width);
    this.setHeight(this.height * height);
  }
  changeProperty(propertyName: string, propertyValue: any) {
    switch (propertyName) {
      case "width":
      case "height":
        this[propertyName] = propertyValue;
        break;
      case "background":
        this[propertyName] = propertyValue;
        break;
      case "isFill":
        this[propertyName] = propertyValue;
        break;
      case "x":
        this.setPosition(propertyValue, this.position.y);
        let rect = this.getRect();
        this.setOriginRect(rect.x, rect.y, rect.width, rect.height);
        break;
      case "y":
        this.setPosition(this.position.x, propertyValue);
        let rect1 = this.getRect();
        this.setOriginRect(rect1.x, rect1.y, rect1.width, rect1.height);
        break;
      default:
        break;
    }
  }
  setZoom(zoom: number = 1) {
    this.zoom = zoom;
    this.setWidth(this.width * zoom);
    this.setHeight(this.height * zoom);
    this.setLeftTop(this.leftTop.x * zoom, this.leftTop.y * zoom);
    const newRect = this.getRect();
    this.setOriginRect(newRect.x, newRect.y, newRect.width, newRect.height);
  }
  getZoom(): number {
    return this.zoom;
  }
}
